﻿using System;
using System.Drawing;

using TrainSimulator.Interfaces;
using System.Collections.Generic;
using TrainSimulator.Interfaces.DataGridView;

namespace TrainSimulator.Model.Track
{
    public abstract class Track : ITrack
    {
        public event TrackChangeHandler OnChange;
        public event BlockHandler OnBlock;

        #region Event class
        // Muligvis uden interface
        // IsBlocked - new and old value
        public class TrackEventArgs : ITrackEventArgs
        {
            public ITrack Track { get; set; }
            public object OldValue { get; set; }
            public object NewValue { get; set; }

            public TrackEventArgs(ITrack t, object oldv, object newv)
            {
                this.Track = t;
                this.OldValue = oldv;
                this.NewValue = newv;
            }
        }
        #endregion

        public int ID { get; set; }
        public double Length { get; private set; }
        public double StartAngle { get; set; }
        public double Angle { get; set; }
        public ITrack NextTrack { get; set; }
        public ITrack Previous { get; set; }
        public string NextInfo { get { return (NextTrack != null) ? NextTrack.ID.ToString("ID: 0") : ""; } }
        public string PreviousInfo { get { return (Previous != null) ? Previous.ID.ToString("ID: 0") : ""; } }
        public ITrack[] NextTracks { get { ITrack[] a = { this.NextTrack }; return a; } }
        string ITrack.Blocked { get { return IsBlocked ? "Blocked" : "Free"; } }
        string IDgvTrack.Blocked { get { return IsBlocked ? "Blocked" : "Free"; } }
        private bool _isBlocked = false;
        public bool IsBlocked
        {
            get { return _isBlocked; }
            set
            {


                bool newValue = (Boolean)value;
                bool oldValue = _isBlocked;
                if (oldValue != newValue)
                {
                    _isBlocked = newValue;
                    BlockHandler tempBlockEvent = OnBlock;
                    if (tempBlockEvent != null)
                    {// SPØRG PETER!!!
                        ITrackEventArgs args = new TrackEventArgs(this, oldValue, newValue);
                        tempBlockEvent(this, args);
                    }
                    //foreach (ISensor s in Sensors)
                    //{
                    //    if (s is SwitchSensor) s.Value = newValue;
                    //}
                    FireChange(oldValue, newValue);
                }
            }
        } // Is on the track

        public ITrain BlockingTrain { get; set; }
        //ITrainSet _train;
        //ArrayList _signals;
        //ArrayList _sensors;


        // Geometry
        public Bitmap TrackImage { get; set; }
        public PointF StartPoint { get; set; }
        public PointF EndPoint { get; set; }

        public Track(int id, double length, double startAngle, double angle)
        {
            ID = id;
            Length = length;
            StartAngle = startAngle;
            Angle = angle;
            NextTrack = null;
            Previous = null;
            IsBlocked = false; // No trains on tracks from beginning
        }

        protected void FireChange(Object oldValue, Object newValue)
        {
            TrackChangeHandler tempChangeEvent = OnChange;
            if (tempChangeEvent != null)
            {
                ITrackEventArgs args = new TrackEventArgs(this, oldValue, newValue);
                tempChangeEvent(this, args);
            }
        }

        public override string ToString()
        {
            return string.Format("ID: {0}, Length: {1}", ID, Length);
        }
    }
}
